<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="umsroot.css">
<TITLE>
Introduction
</TITLE>
</HEAD>
<BODY >
<A HREF="umsroot121.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot123.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc265">19.1</A>&nbsp;&nbsp;Introduction</H2><UL>
<LI><A HREF="umsroot122.html#toc145">The Shared/Private Heap</A>
<LI><A HREF="umsroot122.html#toc146">The Local Stack</A>
<LI><A HREF="umsroot122.html#toc147">The Control Stack</A>
<LI><A HREF="umsroot122.html#toc148">The Global Stack</A>
<LI><A HREF="umsroot122.html#toc149">The Trail Stack</A>
</UL>

This chapter may be skipped on a first reading.
Its purpose is to give the advanced user a better understanding
of how the system uses memory resources.
In a high level language like Prolog it is often not obvious for the programmer
to see where the system allocates or frees memory.<BR>
<BR>
The sizes of the different memory areas can be queried by means of the predicate
<A HREF="../bips/kernel/env/statistics-2.html"><B>statistics/2</B></A><A NAME="@default1064"></A> and <A HREF="../bips/kernel/env/statistics-0.html"><B>statistics/0</B></A><A NAME="@default1065"></A> prints a summary of all these data.
Here is a sample output:
<PRE CLASS="verbatim">
[eclipse 1]: statistics.

times:                  [1.12, 0.09, 2.74] seconds
session_time:           2.74 seconds
event_time:             2.74 seconds
global_stack_used:      1936 bytes
global_stack_allocated: 4456448 bytes
global_stack_peak:      4456448 bytes
trail_stack_used:       64 bytes
trail_stack_allocated:  262144 bytes
trail_stack_peak:       4456448 bytes
control_stack_used:     564 bytes
control_stack_allocated:262144 bytes
control_stack_peak:     262144 bytes
local_stack_used:       492 bytes
local_stack_allocated:  262144 bytes
local_stack_peak:       262144 bytes
shared_heap_allocated:  1613824 bytes
shared_heap_used:       1411000 bytes
private_heap_allocated: 73728 bytes
private_heap_used:      36992 bytes
gc_number:              1 
gc_collected:           23472.0 bytes
gc_area:                23560 bytes
gc_ratio:               99.6264855687606 %
gc_time:                0.0 seconds
dictionary_entries:     3252 
dict_hash_usage:        2117 / 8192 
dict_hash_collisions:   314 / 2117 
dict_gc_number:         2 
dict_gc_time:           0.01 seconds
</PRE>The <B>used</B>-figures indicate the actual usage at the moment the
statistics built-in was called. The <B>allocated</B> value is the
amount of memory that is reserved for this area and actually occupied
by the ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> process. The <B>peak</B> value indicates what was the
maximum allocated amount during the session.
In the following we will discuss the six memory areas mentioned.
The <TT>gc</TT>-figures are described in section <A HREF="umsroot123.html#gc">19.2</A>.<BR>
<BR>
<A NAME="toc145"></A>
<H3 CLASS="subsection"><A NAME="htoc266">19.1.1</A>&nbsp;&nbsp;The Shared/Private Heap</H3>
<A NAME="@default1066"></A>
<A NAME="@default1067"></A>
<A NAME="@default1068"></A>
The heap is used to store a variety of data:
<UL CLASS="itemize"><LI CLASS="li-itemize">
<B>compiled code:</B>
The heap is used to store compiled Prolog code.
Consequently its size is increased by the various <A HREF="../bips/kernel/compiler/compile-1.html"><B>compile</B></A><A NAME="@default1069"></A>-predicates,
the <A HREF="../bips/kernel/dynamic/assert-1.html"><B>assert</B></A><A NAME="@default1070"></A>-family and by <A HREF="../bips/kernel/externals/load-1.html"><B>load/1</B></A><A NAME="@default1071"></A>.
Space is freed when single clauses (<A HREF="../bips/kernel/dynamic/retract-1.html"><B>retract</B></A><A NAME="@default1072"></A>) or
whole predicates (<A HREF="../bips/kernel/compiler/abolish-1.html"><B>abolish</B></A><A NAME="@default1073"></A>) are removed from the system.
Note that space reclaiming is usually delayed in these cases
(see <A HREF="../bips/kernel/env/trimcore-0.html"><B>trimcore/0</B></A><A NAME="@default1074"></A>),
since the removed code may still be under execution. 
Erasing a module also reclaims all the memory occupied by the module's
predicates.
<LI CLASS="li-itemize"><B>nonlogical storage:</B>
All facilities for storing information across backtracking use the
heap to do so. This includes the handle-based facilities
(bags, shelves) as well as the name-based facilities (records, nonlogical
variables and arrays).
<A NAME="@default1075"></A>
<A NAME="@default1076"></A>
<A NAME="@default1077"></A>
<A NAME="@default1078"></A>
As a general rule, when a stored term is overwritten, the space for
the old value is reclaimed. All memory related to a nonlogical store is
reclaimed when the store is destroyed (e.g. using
<A HREF="../bips/kernel/storage/erase_array-1.html"><B>erase_array/1</B></A><A NAME="@default1079"></A>,
<A HREF="../bips/kernel/record/erase_all-1.html"><B>erase_all/1</B></A><A NAME="@default1080"></A>,
<A HREF="../bips/kernel/storage/bag_abolish-1.html"><B>bag_abolish/1</B></A><A NAME="@default1081"></A>,
<A HREF="../bips/kernel/storage/shelf_abolish-1.html"><B>shelf_abolish/1</B></A><A NAME="@default1082"></A>).
<LI CLASS="li-itemize"><B>dictionary:</B>
<A NAME="@default1083"></A>
The <I>dictionary</I> is the system's table of atoms and functors.
The dictionary grows whenever the system encounters an atom or functor that
has not been mentioned so far.
The dictionary shrinks on dictionary garbage collections, which are triggered
automatically after a certain number of new entries has been made
(see <A HREF="../bips/kernel/env/set_flag-2.html"><B>set_flag/2</B></A><A NAME="@default1084"></A>).
The dictionary is designed to hold several thousand entries,
the current number of entries can be queried with <A HREF="../bips/kernel/env/statistics-0.html"><B>statistics/0,2</B></A><A NAME="@default1085"></A>.
<LI CLASS="li-itemize"><B>various descriptors:</B>
The system manages a number of other internal tables (for modules, predicates,
streams, operators, etc.) that are also allocated on the heap.
This space is reclaimed when the related Prolog objects cease to exist.
<LI CLASS="li-itemize"><B>I/O-buffers:</B>
When streams are opened, the system allocates buffers from the
heap. They are freed when the stream is closed.
<LI CLASS="li-itemize"><B>allocation in C-externals:</B>
If third party libraries or external predicates written in C/C++ call
malloc() or related C library functions, this space is also allocated
from the heap. It is the allocating code's responsibility to free
this space if it becomes unused.
</UL>
Note that the distinction between shared and private heap is only relevant for
parallel ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> systems, where multiple workers share the shared
heap, but have their own private heap and stacks.<BR>
<BR>
<A NAME="toc146"></A>
<H3 CLASS="subsection"><A NAME="htoc267">19.1.2</A>&nbsp;&nbsp;The Local Stack</H3>
<A NAME="@default1086"></A>
<A NAME="@default1087"></A>
The Local Stack is very similar to the call/return stack in procedural
languages.
It holds Prolog variables and return addresses.
Space on this stack is allocated during execution of a clause and deallocated
before the last subgoal is called (due to tail recursion / last call
optimisation).
This deallocation can not be done when the clause exits nondeterministically
(this can be checked with the debugger or the profiling facility).
However, if a deallocation has been delayed due to nondeterminism, it is
finally done when a cut is executed or when execution fails beyond
the allocation point.
Hence the ways to limit growth of the local stack are
<UL CLASS="itemize"><LI CLASS="li-itemize">
use tail recursion where possible
<LI CLASS="li-itemize">avoid unnecessary nondeterminism (cf. <A HREF="#controlstack">19.1.3</A>)
</UL>
<A NAME="toc147"></A>
<H3 CLASS="subsection"><A NAME="htoc268">19.1.3</A>&nbsp;&nbsp;The Control Stack</H3>
<A NAME="@default1088"></A>
<A NAME="@default1089"></A>
<A NAME="controlstack"></A>
The main use of the Control Stack is to store so-called <I>choicepoints</I>.
A choicepoint is a description of the system's state at a certain point
in execution.
It is created when more than one clause of a predicate apply to a given goal.
Should the first clause fail, the system will backtrack
to the place where the choice was made, the old state will be restored
from the choicepoint and the next clause will be tried.
Disjunctions (<A HREF="../bips/kernel/control/O-2.html"><B>;/2</B></A><A NAME="@default1090"></A>) also create choicepoints.<BR>
<BR>
The only way to reduce Control Stack usage is to avoid unnecessary
nondeterminism.
This is done by writing deterministic predicates in such a way that they
can be recognised by the system.
The debugger can help to identify nondeterministic predicates:
When it displays an <TT>*EXIT</TT> port instead of <TT>EXIT</TT> then the predicate
has left a choicepoint behind.
In this case it should be checked whether the nondeterminism was intended.
If not, the predicate can often be made deterministic by
<UL CLASS="itemize"><LI CLASS="li-itemize">
writing the clause heads such that a matching clause can be more
easily selected by <I>indexing</I>
<LI CLASS="li-itemize">using the if-then-else construct (<TT>.. -&gt; .. ; ..</TT>)
<LI CLASS="li-itemize">deliberate insertion of (green) cuts
</UL>
<A NAME="toc148"></A>
<H3 CLASS="subsection"><A NAME="htoc269">19.1.4</A>&nbsp;&nbsp;The Global Stack</H3>
<A NAME="@default1091"></A>
The Global Stack holds Prolog structures, lists, strings and long numbers.
So the user's selection of data structures is largely responsible
for the growth of this stack (cf. <A HREF="umsroot025.html#chapstring">5.4</A>).
In coroutining mode, delayed goals also consume space on the Global Stack.
It also stores source variable names for terms which
were read in with the flag <B>variable_names</B> being <TT>on</TT>.
When this feature is not needed, it should be turned off
so that space on the global stack is saved.<BR>
<BR>
The global stack grows while a program creates data structures.
It is popped only on failure. ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> therefore provides a garbage collector
for the Global Stack which is called when a certain amount
of new space has been consumed. See section <A HREF="umsroot123.html#gc">19.2</A> for how this process
can be controlled.
Note again that unnecessary nondeterminism reduces the amount of garbage
that can be reclaimed and should therefore be avoided.<BR>
<BR>
<A NAME="toc149"></A>
<H3 CLASS="subsection"><A NAME="htoc270">19.1.5</A>&nbsp;&nbsp;The Trail Stack</H3>
<A NAME="@default1092"></A>
The Trail Stack is used to record information that is needed on backtracking.
It is therefore closely related to the Control Stack.
Ways to reduce Trail Stack consumption are
<UL CLASS="itemize"><LI CLASS="li-itemize">
avoid unnecessary nondeterminism
<LI CLASS="li-itemize">supply <B>mode</B> declarations
</UL>
The Trail Stack is popped on failure and
is garbage collected together with the Global Stack.
<BR>
<BR>
<HR>
<A HREF="umsroot121.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot123.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
